package com.moco.launcher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.MotionEvent;

import com.moco.launcher.LauncherApplication;
import com.moco.launcher.R;
import com.moco.launcher.fragment.VolumeFragment;
import com.moco.launcher.listener.FragmentListener;
import com.oazon.common.SettingType;
import com.oazon.common.Settings;
import com.oazon.common.Logger;
import com.oazon.common.Assert;

public class VolumeActivity extends BaseFragmentActivity implements
		FragmentListener {
	private static final String TAG = "VolumeActivity";
	
	private LauncherApplication mApp = null;
	private VolumeFragment fragment;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		setContentView(R.layout.pop_layout);

		Intent intent = this.getIntent();
		String type = intent.getStringExtra("fragment_type");
		Logger.d(TAG, "type=" + type);
		if (type.equals("volume")) {
			fragment = new VolumeFragment();
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, fragment).commit();
		}	
		Assert.notNull(fragment, "fragment is Null in VolumeActivity");
		fragment.registerCallback(this);
		
		mApp = ((LauncherApplication) getApplication());
		mApp.registerActivty(this);
		mApp.resetStartTime();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		
		setIntent(intent);
		
		Fragment fragment = getSupportFragmentManager().getFragments().get(0);
		if (fragment instanceof VolumeFragment) {
			((VolumeFragment) fragment).adjustVolumeProgree(intent.getIntExtra(
					"volume", Settings.getInstance(mApp).getInt(SettingType.VOLUME.value(),13)));
		}
	}

	@Override
	public void finishActivity() {
		this.finish();
		
		overridePendingTransition(R.anim.pop_in, R.anim.pop_out);
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		Logger.d(TAG, "the launcher receive dispatchTouchEvent event");
		mApp.resetStartTime();
		
		return super.dispatchTouchEvent(event);
	}
}
